﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Codemo.Core.Applications
{
    /// <summary>
    /// 
    /// </summary>
    public class Application
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Application"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="packageRepositoryPath">The package repository path.</param>
        public Application(string name, string packageRepositoryPath)
        {
            Name = name;
            PackageRepositoryPath = packageRepositoryPath;
            Versions = new List<DeploymentConfigurationVersion>();
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; private set; }

        /// <summary>
        /// Gets or sets the package repository path.
        /// </summary>
        /// <value>The package repository path.</value>
        public string PackageRepositoryPath { get; set; }

        /// <summary>
        /// Gets the versions.
        /// </summary>
        /// <value>The versions.</value>
        public IList<DeploymentConfigurationVersion> Versions { get; private set; }

        /// <summary>
        /// Gets the temporary directory.
        /// </summary>
        /// <value>The temporary directory.</value>
        public DirectoryInfo TemporaryDirectory
        {
            get { return new DirectoryInfo(Path.Combine(Path.GetTempPath(), Name)); }
        }

        /// <summary>
        /// Finds the version.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <returns></returns>
        public DeploymentConfigurationVersion FindVersion(string version)
        {
            foreach (DeploymentConfigurationVersion applicationVersion in Versions)
            {
                if (applicationVersion.Label == version)
                {
                    return applicationVersion;
                }
            }

            throw new InvalidOperationException(string.Format("Invalid version label : {0}", version));
        }
    }
}